<template>
  <!-- 横向柱状图界面 -->
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        xAxis: {
          max: 'dataMax',
          name: "数量"
        },
        yAxis: {
          type: 'category',
          data: ['沂南县', '沂水县', '蒙阴县'],
          inverse: true,  //表示 Y 轴从下往上是从小到大的排列
          animationDuration: 300,   //表示第一次柱条排序动画的时长
          animationDurationUpdate: 300,
          max: 2,    // only the largest 3 bars will be displayed
        },
        series: [
          {
            realtimeSort: true, //开启该系列的动态排序效果
            name: '超载区',
            type: 'bar',
            data: [200, 300, 500],
            label: {
              show: true,  //显示数值
              position: 'right',
              valueAnimation: true
            },
            itemStyle: {
              color: '#ee5656'
            }
          },
          {
            // realtimeSort: true,
            name: '临界区',
            type: 'bar',
            data: [50, 345, 322],
            label: {
              show: true,
              position: 'right',
              valueAnimation: true
            },
            itemStyle: {
              color: '#7cadef'
            }
          },
          {
            //realtimeSort: true,
            name: '盈余区',
            type: 'bar',
            data: [125, 222, 444],
            label: {
              show: true,
              position: 'right',
              valueAnimation: true
            },
            itemStyle: {
              color: '#9de182'
            }
          }
        ],
        legend: {
          data: ["超载区", "临界区", "盈余区"],
          show: true
        },
        animationDuration: animationDuration,
        animationDurationUpdate: animationDuration,
        animationEasing: 'linear',
        animationEasingUpdate: 'linear'
      })
    }
  }
}
</script>
